<template>
  <div class="synchronouSpreadstyle">
    <!-- 标题 -->
    <h2>{{ datas.text }}</h2>
    <el-form label-width="98px" :model="datas" size="small">
      <!-- 类型列表 -->
      <div>
        <h5 style="color: #000; font-size: 14px">添加内容</h5>
        <p style="color: #969799; font-size: 12px; margin-top: 10px">
          拖动选中的内容可对其排序
        </p>
        <el-form-item label="单品包装样式">
          <el-radio-group v-model="datas.appListType">
            <el-radio
              v-for="item in appTypeList"
              :label="item.value"
              :key="item.value">
              {{ item.name }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="同步样式">
          <el-radio-group v-model="datas.applicationType">
            <el-radio
              v-for="item in applicationTypeList"
              :label="item.value"
              :key="item.value">
              {{ item.name }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item class="lef" label="显示标题">
          <el-radio-group v-model="datas.isShowTab">
            <el-radio :label="0">隐藏</el-radio>
            <el-radio :label="1">显示</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="栏目标题" prop="synTitle" class="lef" :hide-required-asterisk="true">
          <el-input placeholder="请输入栏目标题" v-model="datas.synTitle" clearable></el-input>
        </el-form-item>
        <el-form-item label="栏目描述" prop="synDesc" class="lef" :hide-required-asterisk="true">
          <el-input placeholder="请输入栏目描述" v-model="datas.synDesc" clearable></el-input>
        </el-form-item>
        <el-form-item v-show="datas.isShowTab === 1" label="左侧标题" prop="tabNameLeft" class="lef" :hide-required-asterisk="true">
          <el-input placeholder="请输入左侧标题" v-model="datas.tabNameLeft" clearable></el-input>
        </el-form-item>
        <el-form-item v-show="datas.isShowTab === 1" label="右侧标题" prop="tabNameRight" class="lef" :hide-required-asterisk="true">
          <el-input placeholder="请输入右侧标题" v-model="datas.tabNameRight" clearable></el-input>
        </el-form-item>
        <vuedraggable v-model="datas.imageList" v-bind="dragOptions">
          <transition-group>
            <section v-for="(item, index) in datas.imageList" :key="item + index" class="imgBanner">
              <i class="el-icon-circle-close" @click="deleteimg(index)" />
              <!-- 图片 -->
              <div class="imag">
                <img src="../../../assets/images/add.png" @click="showUpload('second', index)" alt draggable="false" v-if="!item.src" />
                <img :src="item.src" @click="showUpload('second', index)" alt draggable="false" v-else />
              </div>
              <!-- 标题和链接 -->
              <div class="imgText">
                <el-input v-model="item.title" placeholder="请输入标题" size="mini" clearable />
                <div style="height: 5px" />
                <el-input v-model="item.desc" placeholder="请输入描述" size="mini" clearable />
                <div style="height: 5px" />
                <!-- 选择链接 -->
                <select-link @selectThisLink="selectThisLink" @clearThisLink="clearThisLink(index)" :linktypeurl="item.url" :theIndex="index" />
                <el-form-item class="lef" label="VIP标识">
                  <el-radio-group v-model="item.isVipShow">
                    <el-radio :label="2">隐藏</el-radio>
                    <el-radio :label="1">显示</el-radio>
                  </el-radio-group>
                </el-form-item>
              </div>
            </section>
          </transition-group>
        </vuedraggable>
        <el-button class="uploadImg" type="primary" plain @click="addMessage">
          <i class="el-icon-plus" />点击添加内容
        </el-button>
      </div>

      <el-form-item label="选择学段">
        <el-radio-group v-model="datas.sectionCode">
          <el-radio
            v-for="item in templateTagLevelDTOList"
            :label="item.tagEnumCode + '-' + item.tagEnumName"
            :key="item.tagEnumCode"
            @change="chooseSubject"
            style="margin-bottom: 15px">
            {{ item.tagEnumName }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="选择科目" class="lef-height">
        <el-radio-group v-model="datas.subjectCode">
          <el-radio
            v-for="item in newSubjectArr"
            :label="item.tagEnumCode"
            :key="item.tagEnumCode"
            style="margin-bottom: 15px">
            {{ item.tagEnumName }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <!-- 上传图片 -->
    <uploadimg ref="upload" @uploadInformation="uploadInformation" />
  </div>
</template>
  
<script>
import vuedraggable from "vuedraggable";
import SelectLink from "../../selectLink";
import uploadimg from "../../uploadImg"; // 图片上传
import { getTemplateTagLevel } from "@/api/basisMG";
export default {
  name: "SynchronouSpreadstyle",
  components: {
    vuedraggable,
    SelectLink, 
    uploadimg 
  },
  props: {
    datas: Object,
  },
  data() {
    return {
      dragOptions: {
        animation: 200,
      },
      appTypeList: [
        {
          value: 0,
          name:'一行四个'
        },
        {
          value: 1,
          name:'一行两个'
        }
      ],
      applicationTypeList: [
        {
          value: 0,
          name:'一行三个'
        },
        {
          value: 1,
          name:'一行两个'
        }
      ],
      templateTagLevelDTOList: [],
      subjectArr: [],
      sectionListArr: [],
      newSubjectArr: [],
      section: '',
      changeType: "",
      changeIndex: ""
    };
  },
  methods: {
    addMessage() {
      this.datas.imageList.push({
        src: "",
        title: "",
        desc: "",
        isVipShow: 2
      });
    },
    // 链接选择
    selectThisLink(resu) {
      this.datas.imageList.forEach((it, ind) => {
        if (ind == resu.index) {
          it.url = resu.url;
          it.linktype = resu.linktype;
          it.id = resu.id;
          it.title = resu.name;
        }
      });
    },
    clearThisLink(index) {
      this.datas.imageList[index].url = "";
    },
    showUpload(type, index) {
      this.changeType = type;
      this.changeIndex = index;
      this.$refs.upload.showUpload();
    },
    uploadInformation(res) {
      switch (this.changeType) {
        case 'first':
          this.datas.topImage = res;
          break;
        case 'second':
          this.datas.imageList[this.changeIndex].src = res;
          break;
      }
    },
    //获取学段和科目
    async getTemplateTagLevel() {
      let arrSubje = [];
      const res = await getTemplateTagLevel({ id: "1361897479733286" });
      this.templateTagLevelDTOList = res.data.templateTagLevelDTOList;
      this.templateTagLevelDTOList.forEach((item) => {
        arrSubje.push(item.childrenDTOList);
      });
      this.subjectArr = JSON.parse(JSON.stringify(arrSubje));
      this.chooseSubject(this.datas.sectionCode);
    },
    chooseSubject(e) {
      if (e == "0001-小学") {
        this.newSubjectArr = this.subjectArr[0];
      } else if (e == "0002-初中") {
        this.newSubjectArr = this.subjectArr[1];
      } else if (e == "0003-高中") {
        this.newSubjectArr = this.subjectArr[2];
      } else {
        this.newSubjectArr = this.subjectArr[3];
      }
    },
    /* 删除图片 */
    deleteimg(index) {
      this.datas.imageList.splice(index, 1);
    }
  },
  created() {
    this.getTemplateTagLevel();
  }
};
</script>
  
<style scoped lang="scss">
.synchronouSpreadstyle {
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  padding: 0 10px 20px;
  box-sizing: border-box;
  /* 标题 */
  h2 {
    padding: 24px 16px 24px 0;
    margin-bottom: 15px;
    border-bottom: 1px solid #f2f4f6;
    font-size: 18px;
    font-weight: 600;
    color: #323233;
  }
  /* 图片广告列表 */
  .imgBanner {
    padding: 6px 12px;
    margin: 16px 7px;
    border-radius: 2px;
    background-color: #fff;
    box-shadow: 0 0 4px 0 rgba(10, 42, 97, 0.2);
    display: flex;
    position: relative;

    /* 删除图标 */
    .el-icon-circle-close {
      position: absolute;
      right: -10px;
      top: -10px;
      cursor: pointer;
      font-size: 19px;
    }

    /* 图片 */
    .imag {
      width: 60px;
      height: 60px;
      border-radius: 5px;
      overflow: hidden;
      position: relative;
      cursor: pointer;
      img {
        width: 100%;
        height: 100%;
        display: inline-block;
      }
      span {
        background: rgba(0, 0, 0, 0.5);
        font-size: 12px;
        position: absolute;
        left: 0px;
        bottom: 0px;
        display: inline-block;
        width: 100%;
        text-align: center;
        color: #fff;
        height: 20px;
        line-height: 20px;
      }
    }

    /* 图片字 */
    .imgText {
      width: 80%;
      display: flex;
      flex-direction: column;
      box-sizing: border-box;
      padding-left: 20px;
      justify-content: space-between;
      .select-type {
        display: flex;
        .el-select {
          .el-input {
            input {
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
            }
          }
        }
      }
    }
  }
  .uploadImg{
    margin-bottom: 30px;
  }
  /* 背景图 */
  .backgroundImg {
    display: inline-flex;
    align-items: center;
    cursor: pointer;
    width: 80px;
    height: 80px;
    position: relative;
    background: #f2f4f6;
    margin-bottom: 30px;
    img {
      width: 100%;
      height: auto;
    }
    span {
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      height: 30px;
      color: #fff;
      background: rgba(0, 0, 0, 0.5);
      text-align: center;
      line-height: 30px;
    }
  }
  .lef {
    .el-form-item__label {
      text-align: left;
    }
    p {
      line-height: 4px;
    }
  }
  .lef-height {
    .el-form-item__label {
      text-align: left;
      width: 80px;
      float: left;
    }
    .el-form-item__content {
      margin-left: 80px;
    }
  }
}
</style>
<style>
  input::-webkit-outer-spin-button,
  input::-webkit-inner-spin-button {
    -webkit-appearance: none;
  }
  input[type="number"]{
    -moz-appearance: textfield;
    appearance: none;
  }
</style>